import {Injectable} from '@angular/core';
import {CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {LoginService} from './login.service';
import {Observable} from 'rxjs';
import {ActivatedRouteSnapshot} from '@angular/router';

@Injectable()
export class LoginGuard implements CanActivate {
  constructor(private router: Router, private loginService: LoginService) {
  }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    const isLogged = this.loginService.isLogged();
    if (!isLogged) {
      this.router.navigate(['login']);
    }
    return isLogged;
  }
}
